package com.youhui.weixin.service.task;

import com.youhui.weixin.dao.GoodsDao;
import com.youhui.weixin.dao.GoodsLinkDao;
import com.youhui.weixin.domain.Goods;
import com.youhui.weixin.domain.GoodsLink;
import com.youhui.weixin.utils.excel.DealType;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by https://github.com/kuangcp on 17-11-14  下午7:59
 *
 * @author kuangcp
 */
@Service
@Log4j
public class TransTask {

    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private GoodsLinkDao goodsLinkDao;
    @Autowired
    private DealType dealType;

    @Async
    public void transLinkData(int page, int size){

        Pageable pageable = new PageRequest(page, size);
        Page<Goods> goods = goodsDao.findListByLink(pageable);

        Set<String> set = new HashSet<>();
        for (Goods g : goods) {
            String s = dealType.handleLink(g.getGoodsId(), g.getLink());
            goodsLinkDao.save(new GoodsLink(g.getGoodsId(), g.getLink()));
            g.setLink("http://wx.jjyouhuigo.com/s/"+s);
            goodsDao.save(g);
            log.info("http://wx.jjyouhuigo.com/s/"+s);
            set.add(s);
        }
        log.info("Goods:" + goods.getSize()+"  Set:" + set.size());

    }

}
